<template>
  <div class="add-main-version">
    <el-dialog :title="$t('tool.modelManage.addMainVersion')" :visible.sync="dialogFormVisible"
      :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-position="top">
        <el-form-item :label="$t('tool.modelManage.mainVersionName')" prop="mainVersionName">
          <el-input v-model="form.mainVersionName" autocomplete="off" placeholder="v1.0.1,   版本名称不能重复"></el-input>
        </el-form-item>
        <el-form-item :label="$t('tool.modelManage.mainVersionDescription')" prop="mainVersionDescription">
          <el-input v-model="form.mainVersionDescription" autocomplete="off"
            :placeholder="$t('tool.others.placehoderInput')"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="jtyj-btn-cancel" @click="cancel">{{
          $t("setup.buttonText.cancel")
        }}</el-button>
        <el-button class="jtyj-btn-primary" @click="submitForm">{{
          $t("setup.buttonText.save")
        }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addMainVersion } from "@/api/myspace/model-manage-tool/model-manage";
import { mapState } from "vuex";
export default {
  data() {
    return {
      dialogFormVisible: false,
      form: {},
      rules: {
        mainVersionName: [
          { required: true, message: "版本名称不得为空", trigger: "blur" },
        ],
        mainVersionDescription: [
          { required: true, message: "版本描述不得为空", trigger: "blur" },
        ],
      },
    };
  },
  props: {
    modelId: {
      type: Number,
    },
  },
  computed: {
    ...mapState({
      userId: (state) => state.user.id,
    }),
  },
  methods: {
    init() {
      this.dialogFormVisible = true;
      this.reset();
    },
    reset() {
      this.form = {
        mainVersionName: null,
        mainVersionDescription: null,
        modelId: this.modelId,
        userId: this.userId,
      };
      this.resetForm("form");
    },
    cancel() {
      this.dialogFormVisible = false;
    },
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          addMainVersion(this.form).then((response) => {
            if (response.code == 200) {
              this.$message.success(
                this.$toolHandle.getI18nName(
                  this.$zh.setup.responseText,
                  this.$en.setup.responseText,
                  this.$i18n.locale,
                  "success"
                )
              );
              this.$emit("refresh", true);
              this.dialogFormVisible = false;
            }
          });
        }
      });
    },
  },
};
</script>

<style></style>